import { defineStore } from 'pinia';
import { messageFn } from '../../utils';

import {
    preferentialConfigApi,//获取所有特殊处理查询优惠项目下拉
    recoveryDataApi,//获取追收账单前的数据
    recoveryCreateApi,//创建追收账单
} from '/@/api/comprehensive/costRecovery';

import { useLoading } from '/@/components/Loading';

const [openFullLoading, closeFullLoading] = useLoading({
    tip: '加载中...',
});


interface preferentialFace {
    preferentialConfig: []; //优惠方案结果
    total: number;
    pageInfo: object;
    storeBreachMoney: number; //减免违约金额
    reading_list: [] //获取追收账单前的数据
}

export const preferentialConfigStore = defineStore({
    id: 'preferentialConfig-state',
    state: (): preferentialFace => ({
        preferentialConfig: [], //优惠方案结果
        total: 0,//总页数
        pageInfo: {
            //分页
            page_no: 1,
            page_size: 10,
        },
        storeBreachMoney: 0,//减免违约金额
        reading_list: [] //获取追收账单前的数据
    }),
    getters: {

    },
    actions: {
        setTotal(total: number) {
            console.log('setTotal----', total);
            //总页数
            this.total = total;
        },
        setPageInfo(pageParams: object) {
            //设置页面数据
            this.pageInfo = pageParams;
        },
        setStoreBreachMoney(num: number) {
            console.log('setStoreBreachMoney----', num);
            this.storeBreachMoney = num;
        },

        setReading_list(reading_list: []) {
            this.reading_list = reading_list;
        },

        /**
* @description: bookListApi //获取所有特殊处理查询优惠项目下拉
*/
        async preferentialConfigAction() {
            const preferentialConfig = await preferentialConfigApi();
            return preferentialConfig
        },
        /**
* @description: bookListApi //获取追收账单前的数据
*/
        async recoveryDataAction(params: object) {
            const recoveryData = await recoveryDataApi(params);
            if (recoveryData.code === 0) {
                this.setReading_list(recoveryData.reading_list);
            }

            return recoveryData
        },
        /**
* @description:  //创建追收账单
*/
        async recoveryCreateAction(params: object) {
            openFullLoading()
            try {
                const recoveryCreate = await recoveryCreateApi(params);
                if (recoveryCreate.code === 0) {
                    messageFn('success', recoveryCreate.msg);
                    return true;
                } else {
                    messageFn('error', recoveryCreate.msg);
                    return false;
                }
            } finally {
                closeFullLoading()
            }
        },
    }
});
